// import React from 'react';
import BannerHeader from "./comps/BannerHeader";
import GuangGaoOne from "./comps/GuangGaoOne";
import GuangGaoTwo from "./comps/GuangGaoTwo";
import GuangGaoThree from "./comps/GuangGaoThree";
import NavView from "../common/comps/NavView";
import PinPaiArea from "./comps/PinPaiArea";
import ProductSection from "./comps/ProductSection";
import SearchHeader from "../common/comps/SearchHeader";
import HighLevelSearch from "../common/comps/HighLevelSearch";
// import ToastUtils from '../common/ToastUtils'
import HttpUtils from "../common/utils/HttpUtils";
import FixedRightNav from "./comps/FixedRightNav";
import Uts from "@module/Uts";
import PopUp from "./PopUp";

import "./indexpage.css";
import "./zen_init.css";
import EventApi from "@module/EventApi";

class IndexPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            show_high_level_search: false,
            categoryAndProductList: []
        };
        this.callTimes = 0;
    }

    componentDidMount() {
        StateApi.updateStoreState("commonHeaderConfig", { navOpen: true });
        this.getProducts();
            // if(Uts.getCookie('orgId')) {
                this.showAd();
            // }else{
            //     EventApi.on("getIdentity", () => {
            //         this.showAd();
            //     });
            // }
    }

    showAd() {
        // let hasSeeAD = Uts.storage.get("hasSeeAD");
        // let day = new Date().getDate();
        // let month = new Date().getMonth();
        // let year = new Date().getFullYear();

        // if (year >= 2020) {
        //     return;
        // }
        // if (month >= 9) {
        //     return;
        // }
        // if (day >= 24) {
        //     return;
        // }

        if (global.hasLogin) {
            if (
                Uts.getCookie("accrosToken") !=
                Uts.storage.get("loginCacheToken")
            ) {
            //     let validOrgId = [37, 28, 15, 18, 20, 22, 29, 24, 39];
            //     if (Uts.contain(validOrgId, Uts.getCookie("orgId"))) {
                    this.setState({
                        showADAlert: true
                    });
                // }
                //没有看过
            } else {
                /*if (hasSeeAD && hasSeeAD.day != day) {
                    //看过，且保证每天至少看一次
                    this.setState({
                        showADAlert: true
                    });
                }*/
            }
        }
    }

    getProducts() {
        if (
            global.hasLogin &&
            !global.categoriesProducts &&
            this.callTimes < 100
        ) {
            setTimeout(() => {
                this.callTimes++;
                this.getProducts();
            }, 50);
            return;
        }
        if (global.hasLogin && global.categoriesProducts) {
            this.setState({
                categoryAndProductList: global.categoriesProducts
            });
            return;
        }

        HttpUtils.categoryAndProductList()
            .then(res => {
                if (res.code == "0000") {
                    this.setState({
                        categoryAndProductList:
                            res.categoryAndProductList || res.list
                    });
                }
            })
            .catch(err => {
                console.log(err);
            });
    }

    render() {
        let { categoryAndProductList, showADAlert } = this.state;

        let categoryAndProductList_copy = JSON.parse(
            JSON.stringify(categoryAndProductList || [])
        );

        let categoryAndProductList_2 = [];
        if (categoryAndProductList_copy.length > 3) {
            categoryAndProductList_2 = categoryAndProductList_copy.slice(3);
        }

        return (
            <div
                style={{
                    backgroundColor: "rgba(240,245,248,1)",
                    position: "relative"
                }}
            >
                <BannerHeader />

                <div
                    style={{
                        height: 50
                    }}
                />

                {/*<GuangGaoOne/>*/}

                {$state.params.navigationBar != 1 && (
                    <React.Fragment>
                        {categoryAndProductList[0] && (
                            <ProductSection
                                index={1}
                                item={categoryAndProductList[0]}
                            />
                        )}

                        <div
                            style={{
                                height: 50
                            }}
                        />
                        {categoryAndProductList[1] && (
                            <ProductSection
                                index={2}
                                item={categoryAndProductList[1]}
                            />
                        )}

                        <div
                            style={{
                                height: 50
                            }}
                        />
                        {/*<GuangGaoTwo/>*/}
                        {categoryAndProductList[2] && (
                            <ProductSection
                                index={3}
                                item={categoryAndProductList[2] || []}
                            />
                        )}
                        <div
                            style={{
                                height: 50
                            }}
                        />

                        {categoryAndProductList_2.map((item, index) => (
                            <div key={index}>
                                <ProductSection
                                    index={index + 4}
                                    item={item || []}
                                />
                                <div
                                    style={{
                                        height: 30
                                    }}
                                />
                            </div>
                        ))}
                        {showADAlert && (
                            <PopUp
                                hide={() => {
                                    // let date = new Date().getDate();
                                    // let month = new Date().getMonth();
                                    // let fullyear = new Date().getFullYear();
                                    Uts.storage.set(
                                        "loginCacheToken",
                                        Uts.getCookie("accrosToken")
                                    );
                                    this.setState({
                                        showADAlert: false
                                    });
                                }}
                            />
                        )}

                        {/*<div style={{height:30}}/>*/}
                        {/*<GuangGaoThree/>*/}

                        {/*<PinPaiArea/>*/}
                    </React.Fragment>
                )}

                <div
                    style={{
                        height: 25
                    }}
                />

                <FixedRightNav
                    categoryAndProductList={categoryAndProductList}
                />
            </div>
        );
    }
}

export default IndexPage;
